import { MessageBox } from 'element-ui'

/**
 * 将后台返回的错误信息格式化后赋值给前端错误信息对象
 * @param {string} attributeName  属性名称
 * @param {object} targetMsg  错误信息对象
 * @param {string} msg  后台返回的错误提示
 */
export function showValidateMsg(attributeName, targetObj, msg) {
  msg = msg.toLowerCase();
  if (msg.indexOf("'" + attributeName + "'") !== -1) {
    targetObj[attributeName] = msg.replace("'" + attributeName + "' 的", "").replace("'" + attributeName + "' ", "");
  }
}

/**
 * 简单对象的深拷贝,不考虑属性是函数
 * @param {object} source 需要进行复制的对象
 * @returns
 */
export function simpleDeepCopy(source) {
  var result = {};
  for (var key in source) {
    result[key] = source[key];
  }
  return result;
}

/**
 * 深拷贝
 * @param {object} source 需要进行复制的对象
 * @returns
 */
export function deepCopy(source) {
  if (typeof source !== "object") {
    return source;
  }
  if (source == null) {
    return source;
  }
  // 开辟一块新的内存空间
  var newObj = source.constructor === Array ? [] : {};
  for (var i in source) {
    // 通过递归实现深层的复制
    newObj[i] = deepCopy(source[i]);
  }
  return newObj;
}

/**
 * 删除操作确认对话框
 * @param {function} callback 回调函数
 */
export function deleteConfirm(callback, msg) {
  if (!msg) {
    msg = '此操作将永久删除该数据, 是否继续?'
  }
  MessageBox.confirm(msg, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
    center: true,
  })
    .then(() => {
      callback();
    })
    .catch(() => { });
}

/**
 * 前端校验通用必填项校验-字符串类型
 * @param {object} rule 规则
 * @param {object} value 值
 * @param {object} callback 回调函数
 */
export function validateFunction(rule, value, callback) {
  if (!value || value === "0" || value.length <= 0) {
    callback(new Error("不能为空值"));
  } else {
    callback();
  }
}

/**
 * 前端校验通用必填项校验-数字类型
 * @param {object} rule 规则
 * @param {object} value 值
 * @param {object} callback 回调函数
 */
export function validateFunctionPositiveNumber(rule, value, callback) {
  if (value <= 0) {
    callback(new Error("必须大于0"));
  } else {
    callback();
  }
}

/**
 * 数组操作-遍历对象数组,查找需要的Item
 * @param {object} tmpArray 需要遍历的对象数组
 * @param {object} targetKey 目标key
 * @param {object} targetValue 目标value, 如果targetValue为null,则只对数组中的非对象值进行直接比对
 */
export function EachArrayFindItem(tmpArray, targetKey, targetValue) {
  var result = {};
  if (targetValue || targetValue !== undefined) {
    tmpArray.forEach((item) => {
      if (item[targetKey] === targetValue) {
        result = item;
        return;
      }
    });
  } else {
    result = null;
    tmpArray.forEach((item) => {
      if (item === targetKey) {
        result = item;
        return;
      }
    });
  }
  return result;
}
export function myLoading() {
  const loading = this.$loading({
    lock: true,
    text: "Loading",
    spinner: "el-icon-loading",
    background: "rgba(0, 0, 0, 0.7)",
  });
  return loading;
}

// 将url上的所有参数删除
export function clearUrl(route) {
  location.replace(location.href.replace(location.hash, `#${route.path}`));
}

export default {
  showValidateMsg,
  simpleDeepCopy,
  deepCopy,
  deleteConfirm,
  validateFunction,
  validateFunctionPositiveNumber,
  EachArrayFindItem,
  myLoading,
  clearUrl
}
